﻿using Minecraft.Config;
using Minecraft.Config.ipConst;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall
{
	public static class StringRequestInfoExt
	{
		/// <summary>
		/// 获取请求实体对象
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="requestInfo"></param>
		/// <returns></returns>
		public static T GetRequestObj<T>(this StringRequestInfo requestInfo,
			MinecraftSession session) where T : BaseReq
		{
			T req = default(T);
			try
			{
				req = EncryptHelper.Decrypt(requestInfo.Body, "server").JsonDeserialize<T>();
				if (JsonConfig.Value.IsConsoleWrite)
				{
					if (JsonConfig.Value.IsTest)
					{
						EnumCommand command = ProtocolHelper.GetCommand(requestInfo.Key);
						if (command != EnumCommand.Heart_HeartData)
						{
							Console.ForegroundColor = ConsoleColor.Green;
							Console.WriteLine();
							Console.WriteLine($"请求参数（时间：{DateTimeOffset.Now.ToStr()}	协议：{command.ToString()}）：");
							Console.WriteLine($"{req.JsonSerialize()}");
							Console.ResetColor();
						}
					}
				}
			}
			catch (FormatException ex)
			{
				session.Logger.Fatal(ex.Message);
			}
			catch (Newtonsoft.Json.JsonSerializationException ex)
			{
				session.Logger.Fatal(ex.Message);
			}
			catch (Jil.DeserializationException ex)
			{
				session.Logger.Fatal(ex.Message);
			}
			catch (Exception ex)
			{
				session.Logger.Fatal(ex.Message);
			}
			return req;
		}
	}
}
